/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/

package virtPresenter.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;
	
	import virtPresenter.business.SearchXMLDelegate;
	import virtPresenter.event.LoadLogXMLEvent;
	import virtPresenter.model.virtPresenterLocator;	
	
	/**
	 * Commands handles a LoadSearchXMLEvent and loads the search.xml
	 * for the folder, given by the event.	
	 */	
	public class LoadSearchXML implements ICommand, IResponder
	{	
		/**
		 * Instance of the model.
		 */
		private var model:virtPresenterLocator=virtPresenterLocator.getInstance();
		
		/**
		 * Execute method is invoked by the LoadSearchXMLEvent. Calls
		 * its delegate to recieve the result of the search.xml file.
		 * 
		 * @parameters event LoadSearchXMLEvent
		 * 
		 * @see virtPresenter.event.LoadSearchXMLEvent
		 */
		public function execute(event:CairngormEvent):void
		{
			var delegate:SearchXMLDelegate = new SearchXMLDelegate(this);
			delegate.getSearchXML();					
		}
		
		/**
		 * Result method is called by the SearchXMLDelegate and stores the results
		 * of search.xml as an ArrayCollection in the model. 
		 * Throws a LoadLogXMLEvent.
		 *  
		 * @parameters data Data Object that contains the results of the request to the search.xml
		 * 
		 * @see virtPresenter.business.SearchXMLDelegate
		 */
		public function result(data:Object):void
		{	
			trace("LoadSearchXMLCommand. Search.xml loaded!");		
			model.searchCollection = ArrayCollection(data.result.ppt_document.slide);
			//Ist redundant, man kann sich auch dazu entscheiden nur ArrayCollections zu haben
			//oder auch nur XML objekte
			model.searchXML = new XML(data.result.ppt_document.slide);
			model.totalNumberOfSlides=model.searchCollection.length;
			//trace("LoadSearchXML. Number of slides: "+model.searchCollection.length);
			CairngormEventDispatcher.getInstance().dispatchEvent(new LoadLogXMLEvent(model.lectureFolder));				
		}
		
		/**
		 * Fault method is invoked by SearchXMLDelegate if an error occured
		 * loading the search.xml file.
		 * 
		 * @parameters info Info object that contains information about the fault
		 * 
		 * @see virtPresenter.business.SearchXMLDelegate  
		 */
		public function fault(info:Object):void
		{
			trace("LoadSearchXML. Fault in loading search.xml:"+info.toString());
		}		
		
	}
}